package com.pipiqiang.qcamera.app;
import com.pipiqiang.qcamera.R;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.app.KeyguardManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    private static final String TAG = "MainActivity";
    private static final int PERMISSION_REQUEST_CODE = 100;
    
    private ProgressButton btnStartStop; // 修改为ProgressButton
    private Button btnSettings;
    private Button btnGallery;
    private Button btnFlashToggle; // 闪光灯切换按钮（文本按钮）
    private Button btnSelectCamera; // 选择摄像头按钮
    private TextView tvStatus;
    private ImageView appIcon; // 应用图标
    private LinearLayout appIconContainer; // 图标容器
    private TextView tvCaptureCount; // 拍照计数器显示
    private TextView tvCaptureTime; // 拍摄时间显示
    private TextView tvUploadStatus; // 上传状态显示
    private boolean isRunning = false;
    
    // 相机预览相关
    private FrameLayout cameraPreviewContainer;
    private TextureView textureView;
    private ImageView ivCapturedImage; // 用于显示拍摄照片的ImageView
    private ImageButton btnFlashIndicator; // 闪光灯指示器（图标）
    private Button btnScreenOff; // 息屏按钮
    private View blackOverlay; // 息屏覆盖层
    private Handler backgroundHandler;
    private HandlerThread backgroundThread;
    private String cameraId;
    private Size previewSize;
    private boolean isFlashOn = false; // 仅用于预览时的闪光灯指示
    private FlashMode flashModeForCapture = FlashMode.OFF; // 闪光灯三态：关/开/自动
    private boolean isPreviewShowing = false;
    private boolean isScreenOffMode = false;
    
    // 自定义相机管理器
    private CustomCameraManager customCameraManager;
    
    // 进度条相关
    private Handler progressHandler = new Handler();
    private Runnable progressRunnable;
    private long startTime;
    private long interval; // 从设置中获取的间隔时间（毫秒）
    // 方案A：Activity内驱动的拍照循环
    private Handler captureHandler = new Handler();
    private Runnable captureCycleRunnable;
    private long previewWarmupMs = 1200L; // 预览暖机时间，毫秒
    
    // 电源管理
    private PowerManager.WakeLock wakeLock;
    
    // 多摄像头支持
    private String[] cameraIds;
    private int currentCameraIndex = 0;
    private String[] cameraNames; // 摄像头名称
    
    // 拍照计数器
    private CaptureCounter captureCounter;
    
    // 广播接收器，用于接收拍照服务的状态更新
    private BroadcastReceiver serviceStatusReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("com.pipiqiang.qcamera.SERVICE_STATUS".equals(action)) {
                boolean isServiceRunning = intent.getBooleanExtra("isRunning", false);
                updateUI(isServiceRunning);
            }
        }
    };
    
    // 新增广播接收器，用于接收拍照完成的通知
    private BroadcastReceiver captureCompletedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("com.pipiqiang.qcamera.action.CAPTURE_COMPLETED".equals(action)) {
                // 更新计数器显示
                updateCaptureCountDisplay();
                // 照片已在onCaptureSuccess中显示，相机已关闭
                // 下一次拍照周期已在onCaptureSuccess中调度
            } else if ("com.pipiqiang.qcamera.COUNT_RESET".equals(action)) {
                // 计数器重置
                updateCaptureCountDisplay();
            } else if ("com.pipiqiang.qcamera.action.SHOW_LAST_IMAGE".equals(action)) {
                String path = intent.getStringExtra("photoPath");
                showLastCapturedImage(path);
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppLogger.d(TAG, "应用启动");
        
        // 初始化计数器
        captureCounter = new CaptureCounter(getApplicationContext());
        
        // 设置窗口标志以支持锁屏状态下运行
        setupWindowFlags();
        
        // 获取电源锁
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CameraApp::MyWakelockTag");
        
        // 确保ActionBar被隐藏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        
        initViews();
        setupListeners();
        setupUploadStatusObserver();
        
        // 注册广播接收器
        IntentFilter filter = new IntentFilter("com.pipiqiang.qcamera.SERVICE_STATUS");
        registerReceiver(serviceStatusReceiver, filter);
        
        // 注册拍照完成广播接收器
        IntentFilter captureFilter = new IntentFilter();
        captureFilter.addAction("com.pipiqiang.qcamera.action.CAPTURE_COMPLETED");
        captureFilter.addAction("com.pipiqiang.qcamera.COUNT_RESET");
        captureFilter.addAction("com.pipiqiang.qcamera.action.SHOW_LAST_IMAGE"); // 添加新动作
        registerReceiver(captureCompletedReceiver, captureFilter);
        
        // 检查必要权限
        if (checkPermissions()) {
            // 如果权限已授予，检查服务是否正在运行
            checkServiceStatus();
        }
        
        // 初始化计数器显示
        updateCaptureCountDisplay();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (isScreenOffMode && ev != null && ev.getAction() == MotionEvent.ACTION_DOWN) {
            exitScreenOffMode();
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
    
    // 设置窗口标志以支持锁屏状态下运行
    private void setupWindowFlags() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                            WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON |
                            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }
    
    private void initViews() {
        btnStartStop = findViewById(R.id.btn_start_stop);
        btnSettings = findViewById(R.id.btn_settings);
        btnGallery = findViewById(R.id.btn_gallery);
        btnFlashToggle = findViewById(R.id.btn_flash_toggle); // 初始化闪光灯切换按钮
        btnSelectCamera = findViewById(R.id.btn_select_camera); // 初始化选择摄像头按钮
        tvStatus = findViewById(R.id.tv_status);
        appIcon = findViewById(R.id.app_icon); // 初始化应用图标
        appIconContainer = findViewById(R.id.app_icon_container); // 初始化图标容器
        tvCaptureCount = findViewById(R.id.tv_capture_count); // 初始化计数器显示
        
        // 初始化相机预览相关视图
        cameraPreviewContainer = findViewById(R.id.camera_preview_container);
        textureView = findViewById(R.id.texture_view);
        ivCapturedImage = findViewById(R.id.iv_captured_image); // 初始化ImageView
        btnFlashIndicator = findViewById(R.id.btn_flash_indicator);
        btnScreenOff = findViewById(R.id.btn_screen_off);
        tvCaptureTime = findViewById(R.id.tv_capture_time); // 初始化拍摄时间显示
        tvUploadStatus = findViewById(R.id.tv_upload_status); // 初始化上传状态显示
        blackOverlay = findViewById(R.id.black_overlay);
    }
    
    private void setupListeners() {
        btnStartStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRunning) {
                    stopCapture();
                } else {
                    if (checkPermissions()) {
                        startCapture();
                    } else {
                        Toast.makeText(MainActivity.this, "请授予必要权限", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        
        btnSelectCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCameraSelection();
            }
        });
        
        btnFlashToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleFlashSetting();
            }
        });
        
        btnSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
            }
        });
        
        btnGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PhotoGalleryActivity.class);
                startActivity(intent);
            }
        });

        btnScreenOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enterScreenOffMode();
            }
        });

        if (blackOverlay != null) {
            blackOverlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    exitScreenOffMode();
                }
            });
        }
        
        // 纹理视图表面纹理监听器
        textureView.setSurfaceTextureListener(surfaceTextureListener);
    }
    
    private boolean checkPermissions() {
        AppLogger.d(TAG, "检查权限");
        
        String[] permissions;
        if (Build.VERSION.SDK_INT >= 33) {
            permissions = new String[]{
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            };
        } else {
            permissions = new String[]{
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            };
        }
        
        // 检查是否需要MANAGE_EXTERNAL_STORAGE权限（Android 10及以上）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                // 请求MANAGE_EXTERNAL_STORAGE权限
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
                Toast.makeText(this, "请允许应用访问所有文件权限，以便照片能显示在系统相册中", Toast.LENGTH_LONG).show();
            }
        }
        
        ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
        return true;
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        if (requestCode == PERMISSION_REQUEST_CODE) {
            boolean allGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            
            if (!allGranted) {
                Toast.makeText(this, "缺少必要权限，部分功能可能无法使用", Toast.LENGTH_LONG).show();
            } else {
                AppLogger.d(TAG, "所有权限已授予");
                // 获取可用摄像头列表
                initializeCameras();
            }
        }
    }
    
    // 初始化摄像头列表
    private void initializeCameras() {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            cameraIds = manager.getCameraIdList();
            cameraNames = new String[cameraIds.length];
            
            // 获取每个摄像头的名称
            for (int i = 0; i < cameraIds.length; i++) {
                CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraIds[i]);
                Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
                
                if (facing != null) {
                    switch (facing) {
                        case CameraCharacteristics.LENS_FACING_FRONT:
                            cameraNames[i] = "前置摄像头 " + (i + 1);
                            break;
                        case CameraCharacteristics.LENS_FACING_BACK:
                            cameraNames[i] = "后置摄像头 " + (i + 1);
                            break;
                        case CameraCharacteristics.LENS_FACING_EXTERNAL:
                            cameraNames[i] = "外接摄像头 " + (i + 1);
                            break;
                        default:
                            cameraNames[i] = "摄像头 " + (i + 1);
                            break;
                    }
                } else {
                    cameraNames[i] = "摄像头 " + (i + 1);
                }
            }
            
            AppLogger.d(TAG, "发现 " + cameraIds.length + " 个摄像头");
        } catch (CameraAccessException e) {
            AppLogger.e(TAG, "获取摄像头列表失败", e);
            cameraIds = new String[0];
            cameraNames = new String[0];
        }
    }
    
    // 安全地切换摄像头
    private void switchCameraSafely() {
        // 在后台线程中执行摄像头切换
        if (backgroundHandler != null) {
            backgroundHandler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 关闭当前相机
                        if (customCameraManager != null) {
                            customCameraManager.closeCamera();
                        }
                        
                        // 在主线程中更新UI
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 重新打开相机
                                if (textureView.isAvailable()) {
                                    openCamera(textureView.getWidth(), textureView.getHeight());
                                } else {
                                    textureView.setSurfaceTextureListener(surfaceTextureListener);
                                }
                            }
                        });
                    } catch (Exception e) {
                        AppLogger.e(TAG, "切换摄像头时发生错误", e);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "切换摄像头失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }
    }
    
    // 显示摄像头选择对话框
    private void showCameraSelection() {
        if (cameraIds == null || cameraIds.length == 0) {
            Toast.makeText(this, "未发现可用摄像头", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (cameraIds.length == 1) {
            Toast.makeText(this, "设备只有一个摄像头", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 创建选择对话框
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setTitle("选择摄像头");
        builder.setItems(cameraNames, (dialog, which) -> {
            if (which != currentCameraIndex) {
                currentCameraIndex = which;
                btnSelectCamera.setText(cameraNames[currentCameraIndex]);
                
                // 如果预览正在显示，重新打开相机
                if (isPreviewShowing) {
                    switchCameraSafely();
                }
                
                Toast.makeText(this, "已选择: " + cameraNames[currentCameraIndex], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
    
    // 切换闪光灯设置（用于拍照）
    private void toggleFlashSetting() {
        // OFF -> ON -> AUTO -> OFF 循环
        switch (flashModeForCapture) {
            case OFF:
                flashModeForCapture = FlashMode.ON;
                break;
            case ON:
                flashModeForCapture = FlashMode.AUTO;
                break;
            default:
                flashModeForCapture = FlashMode.OFF;
                break;
        }
        AppLogger.d(TAG, "切换闪光灯设置 - 当前模式: " + flashModeForCapture);
        String modeText = (flashModeForCapture == FlashMode.ON) ? "闪光灯: 开" : (flashModeForCapture == FlashMode.AUTO ? "闪光灯: 自动" : "闪光灯: 关");
        btnFlashToggle.setText(modeText);

        // 更新预览时的闪光灯指示器
        updateFlashIndicator();
        
        // 如果预览正在显示，立即更新相机管理器的闪光模式并重启预览
        if (customCameraManager != null && isPreviewShowing) {
            customCameraManager.setFlashMode(flashModeForCapture);
            // 重启预览以应用新的闪光模式
            if (backgroundHandler != null) {
                backgroundHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (customCameraManager != null) {
                            // 通过重启预览来更新闪光灯设置
                            customCameraManager.restartPreviewForFlash();
                        }
                    }
                });
            }
        }

        String toastText = (flashModeForCapture == FlashMode.ON) ? "开启" : (flashModeForCapture == FlashMode.AUTO ? "自动" : "关闭");
        Toast.makeText(this, "拍照时闪光灯设置: " + toastText, Toast.LENGTH_SHORT).show();
    }
    
    // 更新预览时的闪光灯指示器
    private void updateFlashIndicator() {
        if (btnFlashIndicator != null) {
            btnFlashIndicator.setImageResource(flashModeForCapture == FlashMode.ON ? R.drawable.ic_flash_on : R.drawable.ic_flash_off);
        }
    }
    
    private void startCapture() {
        AppLogger.d(TAG, "开始拍照");
        
        // 更新运行状态
        isRunning = true;
        
        // 获取拍照间隔设置（仅用于进度条显示）
        SettingsManager settingsManager = new SettingsManager(this);
        interval = settingsManager.getCaptureInterval() * 1000L; // 转换为毫秒
        
        // 获取电源锁（无限期，停止时释放）
        try {
            wakeLock.setReferenceCounted(false);
            if (!wakeLock.isHeld()) {
                wakeLock.acquire();
            }
        } catch (Exception ignored) {}

        // 更新UI状态与进度条
        updateUI(true);
        startProgressUpdate();

        // 使用前台服务进行拍照，避免休眠中断
        try {
            Intent intent = new Intent(MainActivity.this, CameraService.class);
            intent.setAction(CameraService.ACTION_START_CAPTURE);
            ContextCompat.startForegroundService(MainActivity.this, intent);
        } catch (Exception e) {
            AppLogger.e(TAG, "启动拍照服务失败", e);
            Toast.makeText(this, "启动拍照服务失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void stopCapture() {
        // 停止前台拍照服务
        try {
            Intent intent = new Intent(MainActivity.this, CameraService.class);
            intent.setAction(CameraService.ACTION_STOP_CAPTURE);
            startService(intent);
        } catch (Exception e) {
            AppLogger.e(TAG, "停止拍照服务失败", e);
        }

        // 释放电源锁
        if (wakeLock != null && wakeLock.isHeld()) {
            try { wakeLock.release(); } catch (Exception ignored) {}
        }
        
        // 停止Activity驱动的拍照循环（不再使用，但确保清理）
        stopCaptureLoop();

        // 关闭相机资源，但保留已显示的图片
        closeCameraResourcesOnlyOnUI();
        isPreviewShowing = false;
        
        // 更新UI状态
        updateUI(false);
        
        // 停止进度条更新
        stopProgressUpdate();
        
        // 重置会话计数
        captureCounter.resetSessionCount();
        updateCaptureCountDisplay();
    }

    // 启动Activity内的拍照循环
    private void startCaptureLoop() {
        // 立即执行一次拍照周期
        if (captureCycleRunnable != null) {
            captureHandler.removeCallbacks(captureCycleRunnable);
        }
        captureCycleRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isRunning) return;
                triggerOneCaptureCycle();
            }
        };
        captureHandler.post(captureCycleRunnable);
    }

    // 停止Activity内的拍照循环
    private void stopCaptureLoop() {
        if (captureCycleRunnable != null) {
            captureHandler.removeCallbacks(captureCycleRunnable);
            captureCycleRunnable = null;
        }
    }

    // 单次拍照周期：短暂预览->拍照
    private void triggerOneCaptureCycle() {
        ensureDeviceReadyForCamera();
        // 显示短暂预览（仅在没有预览显示时才显示）
        if (!isPreviewShowing) {
            showCameraPreview();
            // 等待预览准备就绪后再执行拍照
            // 需要等待相机打开 + 预览创建完成
            waitForCameraReadyAndCapture();
        } else {
            // 如果预览已经显示，确保相机已准备好后直接拍照
            if (customCameraManager != null && customCameraManager.isCameraOpened()) {
                captureHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        executeCaptureAfterPreviewReady();
                    }
                }, 300); // 短暂延迟确保预览稳定
            } else {
                // 如果相机未就绪，等待相机打开
                waitForCameraReadyAndCapture();
            }
        }
    }

    private void ensureDeviceReadyForCamera() {
        try {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            boolean interactive = pm != null && pm.isInteractive();
            boolean keyguardLocked = km != null && km.isKeyguardLocked();
            AppLogger.d(TAG, "设备状态: interactive=" + interactive + ", keyguardLocked=" + keyguardLocked);

            if (!interactive || keyguardLocked) {
                PowerManager.WakeLock wl = pm.newWakeLock(
                        PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
                        "CameraApp::PreCaptureWakeMain");
                wl.acquire(5_000);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {}
                if (wl.isHeld()) wl.release();
                // 重新评估状态，仅用于日志
                interactive = pm.isInteractive();
                keyguardLocked = km.isKeyguardLocked();
                AppLogger.d(TAG, "唤醒后设备状态: interactive=" + interactive + ", keyguardLocked=" + keyguardLocked);
            }
        } catch (Exception e) {
            AppLogger.w(TAG, "确保设备就绪时发生异常(Main)", e);
        }
    }
    
    // 等待相机准备就绪后执行拍照
    private void waitForCameraReadyAndCapture() {
        final int maxRetries = 20; // 最多重试20次
        final int retryDelay = 100; // 每次重试间隔100ms
        
        // 使用一个可变的包装类来存储重试计数
        class RetryCounter {
            int count = 0;
        }
        final RetryCounter retryCounter = new RetryCounter();
        
        Runnable checkAndCapture = new Runnable() {
            @Override
            public void run() {
                if (customCameraManager != null && customCameraManager.isCameraOpened()) {
                    // 相机已准备好，再等待一小段时间让预览稳定
            AppLogger.d(TAG, "相机已就绪，等待预览稳定后拍照");
                    captureHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            executeCaptureAfterPreviewReady();
                        }
                    }, 500); // 额外500ms让预览画面稳定显示
                } else {
                    // 相机还未准备好，继续等待
                    retryCounter.count++;
                    if (retryCounter.count < maxRetries) {
                        captureHandler.postDelayed(this, retryDelay);
                    } else {
                        AppLogger.w(TAG, "等待相机准备超时，尝试执行拍照");
                        executeCaptureAfterPreviewReady();
                    }
                }
            }
        };
        
        captureHandler.postDelayed(checkAndCapture, previewWarmupMs);
    }
    
    // 预览准备就绪后执行拍照
    private void executeCaptureAfterPreviewReady() {
        try {
            if (customCameraManager != null && customCameraManager.isCameraOpened()) {
                // 更新闪光模式到拍照设置
                customCameraManager.setFlashMode(flashModeForCapture);
                AppLogger.d(TAG, "相机已就绪，开始拍照");
                customCameraManager.takePicture();
            } else {
                AppLogger.w(TAG, "拍照管理器未就绪，跳过本次拍照");
                // 如果相机未就绪，尝试重新打开
                if (textureView != null && textureView.isAvailable()) {
                    showCameraPreview();
                    waitForCameraReadyAndCapture();
                }
            }
        } catch (Exception e) {
            AppLogger.e(TAG, "执行拍照异常", e);
        }
    }

    // 仅关闭相机资源，不影响已显示的图片
    // 注意：此方法可能从后台线程调用，不能包含UI操作
    private void closeCameraResourcesOnly() {
        try {
            if (customCameraManager != null) {
                customCameraManager.closeCamera();
                customCameraManager.stopBackgroundThread();
                customCameraManager = null;
            }
        } catch (Exception e) {
                AppLogger.e(TAG, "关闭相机资源失败", e);
        }
        // UI操作已移到调用处的runOnUiThread中，这里不再处理
    }
    
    // 在UI线程中关闭相机资源并隐藏预览视图
    private void closeCameraResourcesOnlyOnUI() {
        closeCameraResourcesOnly();
        if (textureView != null) {
            textureView.setVisibility(View.GONE);
        }
    }

    // 调度下一次拍照
    private void scheduleNextCycle() {
        if (!isRunning) return;
        // 重置进度参考时间
        startTime = System.currentTimeMillis();
        // 在设定间隔后执行下一次周期
        captureHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isRunning) return;
                triggerOneCaptureCycle();
            }
        }, interval);
    }
    
    // 启动进度条更新
    private void startProgressUpdate() {
        // 停止之前的进度更新（如果有的话）
        stopProgressUpdate();
        
        // 设置进度按钮为运行状态
        btnStartStop.setProgressState(true);
        
        // 记录开始时间
        startTime = System.currentTimeMillis();
        
        // 创建进度更新任务
        progressRunnable = new Runnable() {
            @Override
            public void run() {
                if (isRunning) {
                    long elapsed = System.currentTimeMillis() - startTime;
                    float progress = (float) (elapsed % interval) / interval;
                    btnStartStop.setProgress(progress);
                    
                    // 每100毫秒更新一次进度
                    progressHandler.postDelayed(this, 100);
                }
            }
        };
        
        // 启动进度更新
        progressHandler.post(progressRunnable);
    }
    
    // 停止进度条更新
    private void stopProgressUpdate() {
        if (progressRunnable != null) {
            progressHandler.removeCallbacks(progressRunnable);
            progressRunnable = null;
        }
        // 重置进度条
        btnStartStop.setProgress(0f);
        btnStartStop.setProgressState(false);
    }
    
    // 更新计数器显示
    private void updateCaptureCountDisplay() {
        if (tvCaptureCount != null) {
            // 重新加载以获取服务侧的最新计数
            if (captureCounter != null) {
                captureCounter.reloadCounts();
            }
            int sessionCount = captureCounter.getSessionCount();
            tvCaptureCount.setText("已拍张数: " + sessionCount);
        }
    }
    
    private void updateUI(boolean running) {
        isRunning = running;
        btnStartStop.setText(running ? R.string.stop_capture : R.string.start_capture);
        tvStatus.setText(running ? R.string.status_running : R.string.status_stopped);
        
        // 更新计数器显示
        updateCaptureCountDisplay();
        
        // 根据运行状态控制图标显示
        if (running) {
            // 运行时隐藏图标容器
            appIconContainer.setVisibility(View.GONE);
            // 显示摄像头选择按钮
            btnSelectCamera.setVisibility(View.VISIBLE);
            // 显示闪光灯设置按钮
            btnFlashToggle.setVisibility(View.VISIBLE);
            // 显示计数器
            tvCaptureCount.setVisibility(View.VISIBLE);
        } else {
            // 停止时显示图标容器
            appIconContainer.setVisibility(View.VISIBLE);
            // 隐藏摄像头选择按钮
            btnSelectCamera.setVisibility(View.GONE);
            // 隐藏闪光灯设置按钮
            btnFlashToggle.setVisibility(View.GONE);
            // 隐藏计数器
            tvCaptureCount.setVisibility(View.GONE);
        }
    }
    
    // 相机预览相关方法
    private void showCameraPreview() {
        cameraPreviewContainer.setVisibility(View.VISIBLE);
        appIconContainer.setVisibility(View.GONE); // 隐藏图标容器
        btnSelectCamera.setVisibility(View.VISIBLE); // 显示摄像头选择按钮
        btnFlashToggle.setVisibility(View.VISIBLE); // 显示闪光灯设置按钮
        btnScreenOff.setVisibility(View.VISIBLE); // 显示息屏按钮
        tvCaptureCount.setVisibility(View.VISIBLE); // 显示计数器

        // 确保ImageView隐藏，TextureView显示
        if (ivCapturedImage != null) {
            ivCapturedImage.setVisibility(View.GONE);
        }
        if (tvCaptureTime != null) {
            tvCaptureTime.setVisibility(View.GONE);
        }
        if (textureView != null) {
            textureView.setVisibility(View.VISIBLE);
            // 注意：TextureView不支持背景绘制，所以我们移除设置背景颜色的代码
        }
        if (isScreenOffMode && blackOverlay != null) {
            blackOverlay.setVisibility(View.VISIBLE);
            blackOverlay.bringToFront();
        }
    
        // 更新摄像头选择按钮文本
        if (cameraNames != null && cameraNames.length > currentCameraIndex) {
            btnSelectCamera.setText(cameraNames[currentCameraIndex]);
        }
    
        isPreviewShowing = true;
        startBackgroundThread();
    
        // 如果纹理视图已经准备好了，就打开相机
        if (textureView != null && textureView.isAvailable()) {
            openCamera(textureView.getWidth(), textureView.getHeight());
        } else if (textureView != null) {
            textureView.setSurfaceTextureListener(surfaceTextureListener);
        }
    }

    private void hideCameraPreview() {
        cameraPreviewContainer.setVisibility(View.GONE);
        appIconContainer.setVisibility(View.VISIBLE); // 显示图标容器
        btnSelectCamera.setVisibility(View.GONE); // 隐藏摄像头选择按钮
        btnFlashToggle.setVisibility(View.GONE); // 隐藏闪光灯设置按钮
        btnScreenOff.setVisibility(View.GONE); // 隐藏息屏按钮
        tvCaptureCount.setVisibility(View.GONE); // 隐藏计数器
        isPreviewShowing = false;
        
        // 关闭自定义相机管理器
        if (customCameraManager != null) {
            customCameraManager.closeCamera();
            customCameraManager.stopBackgroundThread();
            customCameraManager = null;
        }
        
        stopBackgroundThread();
        
        // 确保ImageView和TextureView都隐藏
        if (ivCapturedImage != null) {
            ivCapturedImage.setVisibility(View.GONE);
        }
        if (tvCaptureTime != null) {
            tvCaptureTime.setVisibility(View.GONE);
        }
        if (textureView != null) {
            textureView.setVisibility(View.GONE);
        }
        if (blackOverlay != null) {
            blackOverlay.setVisibility(View.GONE);
        }
    }

    private void enterScreenOffMode() {
        isScreenOffMode = true;
        try {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = 0.0f;
            getWindow().setAttributes(lp);
        } catch (Exception ignored) {}
        if (blackOverlay != null) {
            blackOverlay.setVisibility(View.VISIBLE);
            blackOverlay.bringToFront();
        }
    }

    private void exitScreenOffMode() {
        isScreenOffMode = false;
        try {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = -1f; // 恢复系统默认亮度
            getWindow().setAttributes(lp);
        } catch (Exception ignored) {}
        if (blackOverlay != null) {
            blackOverlay.setVisibility(View.GONE);
        }
    }
    
    private void startBackgroundThread() {
        // 如果后台线程已经存在，先停止它
        if (backgroundThread != null) {
            stopBackgroundThread();
        }
        
        backgroundThread = new HandlerThread("CameraBackground");
        backgroundThread.start();
        backgroundHandler = new Handler(backgroundThread.getLooper());
    }
    
    private void stopBackgroundThread() {
        if (backgroundThread != null) {
            backgroundThread.quitSafely();
            try {
                backgroundThread.join();
                backgroundThread = null;
                backgroundHandler = null;
            } catch (InterruptedException e) {
            AppLogger.e(TAG, "中断后台线程", e);
            }
        }
    }
    
    private final TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
            if (isPreviewShowing) {
                openCamera(width, height);
            }
        }
        
        @Override
        public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
            // 重新配置相机预览
            if (isPreviewShowing) {
                // 关闭当前相机
                if (customCameraManager != null) {
                    customCameraManager.closeCamera();
                }
                openCamera(width, height);
            }
        }
        
        @Override
        public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
            // 当纹理销毁时，确保关闭相机以释放会话，避免悬挂导致黑屏
            if (customCameraManager != null) {
                customCameraManager.closeCamera();
            }
            return true;
        }
        
        @Override
        public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
            // 忽略
        }
    };
    
    private void openCamera(int width, int height) {
        // 确保在后台线程中执行
        if (backgroundHandler == null) {
            Log.e(TAG, "后台线程未启动");
            return;
        }
        
        backgroundHandler.post(new Runnable() {
            @Override
            public void run() {
                // 使用自定义相机管理器
                if (customCameraManager == null) {
                    AppLogger.d(TAG, "创建新的CustomCameraManager实例");
                    customCameraManager = new CustomCameraManager(MainActivity.this);
                } 
                
                // 更新配置
                customCameraManager.setTextureView(textureView);
                customCameraManager.setSelectedCameraIndex(currentCameraIndex);
                customCameraManager.setFlashMode(flashModeForCapture);
                
                // 设置预览显示回调（不再使用，图片通过文件路径显示）
                customCameraManager.setPreviewDisplayCallback(null);
                
                // 设置拍照回调
                customCameraManager.setCaptureCallback(new CustomCameraManager.CaptureCallback() {
                    @Override
                    public void onCaptureSuccess(String imagePath) {
                        AppLogger.d(TAG, "图片保存成功，准备关闭摄像头");
                        
                        // 按照用户要求的流程：拍照完成后立即关闭摄像头和闪光灯
                        // 在后台线程中关闭相机硬件资源，然后在主线程更新UI
                        if (backgroundHandler != null) {
                            backgroundHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    // 关闭摄像头资源（包括闪光灯）- 这是硬件操作，可以在后台线程进行
                                    closeCameraResourcesOnly();
                                    AppLogger.d(TAG, "拍照完成，已关闭摄像头和闪光灯");
                                    
                                    // 在主线程中更新UI
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            // 隐藏预览视图，确保不再显示预览
                                            if (textureView != null) {
                                                textureView.setVisibility(View.GONE);
                                            }
                                            isPreviewShowing = false;
                                            
                                            // 显示拍摄的照片
                                            showLastCapturedImage(imagePath);
                                        }
                                    });
                                }
                            });
                        } else {
                            // 如果后台线程不可用，直接在主线程关闭
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    closeCameraResourcesOnlyOnUI();
                                    isPreviewShowing = false;
                                    AppLogger.d(TAG, "拍照完成，已关闭摄像头和闪光灯");
                                    
                                    // 显示拍摄的照片
                                    showLastCapturedImage(imagePath);
                                }
                            });
                        }
                        
                        // 递增计数器
                        if (captureCounter != null) {
                            captureCounter.incrementCount();
                            AppLogger.d(TAG, "计数器已递增，当前会话计数: " + captureCounter.getSessionCount());
                        }
                        
                        // 发送广播通知拍照完成（用于更新计数器显示）
        Intent captureCompletedIntent = new Intent("com.pipiqiang.qcamera.action.CAPTURE_COMPLETED");
                        captureCompletedIntent.putExtra("photoPath", imagePath);
                        sendBroadcast(captureCompletedIntent);
                        
                        // 调度下一次拍照周期（在间隔时间后才会打开摄像头）
                        scheduleNextCycle();
                    }
                    
                    @Override
                    public void onCaptureError(Exception e) {
                        AppLogger.e(TAG, "拍照失败", e);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "拍照失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                                // 即使失败也要关闭相机
                                closeCameraResourcesOnlyOnUI();
                                isPreviewShowing = false;
                            }
                        });
                    }
                });
                
                customCameraManager.startBackgroundThread();
                customCameraManager.openCamera();
            }
        });
    }
    
    private static class CompareSizesByArea implements Comparator<Size> {
        @Override
        public int compare(Size lhs, Size rhs) {
            return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
                    (long) rhs.getWidth() * rhs.getHeight());
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 运行时不自动显示预览，以节约电量

        // 更新计数器显示
        updateCaptureCountDisplay();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // 隐藏预览并关闭相机，确保资源正确释放
        if (isPreviewShowing) {
            hideCameraPreview();
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 使用CustomCameraManager关闭相机
        if (customCameraManager != null) {
            customCameraManager.closeCamera();
            customCameraManager.stopBackgroundThread();
        }
        stopBackgroundThread();
        stopProgressUpdate(); // 确保停止进度更新
        
        // 释放电源锁
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
        
        unregisterReceiver(serviceStatusReceiver);
        unregisterReceiver(captureCompletedReceiver); // 取消注册新的广播接收器
        try {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = -1f;
            getWindow().setAttributes(lp);
        } catch (Exception ignored) {}
    }
    
    // 显示拍摄的照片
    private void showCapturedImage(Bitmap bitmap) {
        if (bitmap != null && ivCapturedImage != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // 按照用户要求的流程：关闭摄像头后显示拍摄画面和时间水印
                    // 更新预览状态
                    isPreviewShowing = false;
                    
                    // 显示拍摄的照片，并隐藏预览
                    cameraPreviewContainer.setVisibility(View.VISIBLE);
                    ivCapturedImage.setVisibility(View.VISIBLE);
                    ivCapturedImage.setImageBitmap(bitmap);
                    if (textureView != null) textureView.setVisibility(View.GONE);
                    
                    // 显示当前时间 - 确保时间显示在最上层
                    if (tvCaptureTime != null) {
                        tvCaptureTime.setVisibility(View.VISIBLE);
                        tvCaptureTime.bringToFront(); // 确保时间文本在最上层
                        tvCaptureTime.setText("拍摄时间: " + android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis()));
                        AppLogger.d(TAG, "显示拍摄时间: " + tvCaptureTime.getText());
                    } else {
                        AppLogger.w(TAG, "tvCaptureTime为null，无法显示时间");
                    }

                    if (tvUploadStatus != null) {
                        tvUploadStatus.setVisibility(View.VISIBLE);
                        updateUploadStatus();
                        tvUploadStatus.bringToFront();
                    }
                    
                    // 确保ImageView在TextureView之上，时间文本在ImageView之上
                    ivCapturedImage.bringToFront();
                    if (tvCaptureTime != null) {
                        tvCaptureTime.bringToFront();
                    }
                    
                    AppLogger.d(TAG, "显示拍摄的照片，摄像头已关闭，等待下一次拍照周期");
                    
                    // 不再延迟重新显示预览，相机保持关闭状态
                    // 只有在下一个拍照周期开始时（通过scheduleNextCycle触发）才会重新打开摄像头
                }
            });
        }
    }
    
    // 显示上次拍摄的照片
    private void showLastCapturedImage(String photoPath) {
        AppLogger.d(TAG, "显示上次拍摄的照片: " + photoPath);
        // 显示最后拍摄的照片
        if (photoPath != null && !photoPath.isEmpty()) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // 按照用户要求的流程：关闭摄像头后显示拍摄画面和时间水印
                    // 更新预览状态
                    isPreviewShowing = false;
                    
                    if (ivCapturedImage != null) {
                        cameraPreviewContainer.setVisibility(View.VISIBLE);
                        ivCapturedImage.setVisibility(View.VISIBLE);
                        if (textureView != null) textureView.setVisibility(View.GONE);

                        // 加载并显示图片
                        Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
                        if (bitmap != null) {
                            ivCapturedImage.setImageBitmap(bitmap);
                            AppLogger.d(TAG, "成功加载并显示照片");
                        } else {
                            AppLogger.e(TAG, "无法加载照片: " + photoPath);
                        }

                        // 显示拍摄时间（从文件时间或当前时间）- 确保时间显示在最上层
                        if (tvCaptureTime != null) {
                            long ts = System.currentTimeMillis();
                            try {
                                java.io.File f = new java.io.File(photoPath);
                                if (f.exists()) ts = f.lastModified();
                            } catch (Exception ignore) {}
                            tvCaptureTime.setVisibility(View.VISIBLE);
                            tvCaptureTime.bringToFront(); // 确保时间文本在最上层
                            tvCaptureTime.setText("拍摄时间: " + android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", ts));
                            AppLogger.d(TAG, "显示拍摄时间: " + tvCaptureTime.getText());
                        } else {
                            AppLogger.w(TAG, "tvCaptureTime为null，无法显示时间");
                        }

                        if (tvUploadStatus != null) {
                            tvUploadStatus.setVisibility(View.VISIBLE);
                            updateUploadStatus();
                            tvUploadStatus.bringToFront();
                        }
                        
                        // 确保ImageView在TextureView之上，时间文本在ImageView之上
                        ivCapturedImage.bringToFront();
                        if (tvCaptureTime != null) {
                            tvCaptureTime.bringToFront();
                        }
                    }
                }
            });
        } else {
            AppLogger.w(TAG, "照片路径为空，无法显示");
        }
    }
    
    private void setupUploadStatusObserver() {
        try {
            androidx.work.WorkManager.getInstance(getApplicationContext())
                    .getWorkInfosByTagLiveData(com.pipiqiang.qcamera.app.CloudUploadWorker.TAG_UPLOAD_WORK)
                    .observe(this, infos -> {
                        updateUploadStatus(infos);
                    });
        } catch (Exception e) {
            AppLogger.w(TAG, "注册上传状态观察失败", e);
        }
    }

    private void updateUploadStatus() {
        try {
            java.util.concurrent.Executor exec = java.util.concurrent.Executors.newSingleThreadExecutor();
            androidx.work.WorkManager.getInstance(getApplicationContext())
                    .getWorkInfosByTag(com.pipiqiang.qcamera.app.CloudUploadWorker.TAG_UPLOAD_WORK)
                    .addListener(() -> {
                        try {
                            java.util.List<androidx.work.WorkInfo> infos = androidx.work.WorkManager.getInstance(getApplicationContext())
                                    .getWorkInfosByTag(com.pipiqiang.qcamera.app.CloudUploadWorker.TAG_UPLOAD_WORK)
                                    .get();
                            updateUploadStatus(infos);
                        } catch (Exception ignored) {}
                    }, exec);
        } catch (Exception e) {
            AppLogger.w(TAG, "查询上传状态失败", e);
        }
    }

    private void updateUploadStatus(java.util.List<androidx.work.WorkInfo> infos) {
        if (tvUploadStatus == null) return;
        int pending = 0;
        int running = 0;
        int pct = 0;
        long bps = 0;
        long eta = -1;
        String status = "idle";
        if (infos != null) {
            for (androidx.work.WorkInfo wi : infos) {
                androidx.work.WorkInfo.State s = wi.getState();
                if (s == androidx.work.WorkInfo.State.ENQUEUED) pending++;
                else if (s == androidx.work.WorkInfo.State.RUNNING) {
                    running++;
                    androidx.work.Data d = wi.getProgress();
                    pct = d.getInt("progress_pct", pct);
                    bps = d.getLong("speed_bps", bps);
                    eta = d.getLong("eta_ms", eta);
                    status = d.getString("status");
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append("待上传: ").append(pending + running);
        if (running > 0) {
            sb.append(" 进度: ").append(pct).append("%");
            if (bps > 0) sb.append(" 速度: ").append(formatSpeed(bps));
            if (eta >= 0) sb.append(" 剩余: ").append(formatEta(eta));
        }
        String text = sb.toString();
        final String t = text;
        final int fRunning = running;
        final int fPending = pending;
        final String fStatus = status;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvUploadStatus.setText(t);
                if ("error".equalsIgnoreCase(fStatus)) {
                    tvUploadStatus.setTextColor(android.graphics.Color.RED);
                } else if (fRunning > 0) {
                    tvUploadStatus.setTextColor(android.graphics.Color.CYAN);
                } else if ((fPending + fRunning) > 0) {
                    tvUploadStatus.setTextColor(android.graphics.Color.YELLOW);
                } else {
                    tvUploadStatus.setTextColor(android.graphics.Color.GREEN);
                }
                tvUploadStatus.setVisibility(android.view.View.VISIBLE);
            }
        });
    }

    private String formatSpeed(long bps) {
        double kbps = bps / 1024.0;
        double mbps = kbps / 1024.0;
        if (mbps >= 1.0) return String.format(java.util.Locale.getDefault(), "%.2f MB/s", mbps);
        return String.format(java.util.Locale.getDefault(), "%.1f KB/s", kbps);
    }

    private String formatEta(long etaMs) {
        long sec = etaMs / 1000;
        long m = sec / 60;
        long s = sec % 60;
        if (m > 0) return m + "m" + s + "s";
        return s + "s";
    }
    // 检查服务状态
    private void checkServiceStatus() {
        // 这里可以添加检查服务是否正在运行的逻辑
        // 简单起见，我们假设服务未运行
        updateUI(false);
    }
}